[C#] How can we differentiate between SDK class objects and custom class objects?
        Posted  
        
            by Nayan
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Nayan
        
        
        
        Published on 2010-04-13T06:22:51Z
        Indexed on 
            2010/04/13
            6:33 UTC
        
        
        Read the original article
        Hit count: 450
        
To give an idea of my requirement, consider these classes -
class A { }
class B {
    string m_sName;
    public string Name {
        get { return m_sName; }
        set { m_sName = value; }
    }
    int m_iVal;
    public int Val {
        get { return m_iVal; }
        set { m_iVal = value; }
    }
    A m_objA;
    public A AObject {
        get { return m_objA; }
        set { m_objA = value; }
    }
}
Now, I need to identify the classes of the objects passed to a function
void MyFunc(object obj) {
    Type type = obj.GetType();
    foreach (PropertyInfo pi in type.GetProperties()) {
        if (pi.PropertyType.IsClass) { //I need objects only
            if (!type.IsGenericType && type.FullName.ToLower() == "system.string") {
                object _obj = pi.GetValue(obj, null);
                //do something
            }
        }
    }
}
I don't like this piece of code -
    if (!type.IsGenericType && type.FullName.ToLower() == "system.string") {
because then i have to filter out classes like, System.Int16, System.Int32, System.Boolean and so on.
Is there an elegant way through which I can find out if the object is of a class defined by me and not of system provided basic classes?
© Stack Overflow or respective owner